﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardUI : MonoBehaviour
{
    // Start is called before the first frame update
    public CardProps.ID CurrCardID;
    public int HandIndex;
    private Vector3 m_StaticPos = Vector3.zero;
    private bool m_IsDraging = false;
    private bool m_CanDrag = false;
    private Vector3 m_MouseOffsetInWorld =  Vector3.zero;
    private Vector3 m_CurrPosInScreen;
    private Vector3 m_CurrMousePosInScreen;
    private float m_CardAxisZInScreen;
    private Coroutine m_AlphaControl = null;
    private bool m_IsAlphaDescending = true;
    //private int m_OriginSortingOrder;
    void Start() {
    }

    // Update is called once per frame

    //Active when animation end.
    public void DragActive() {
        m_StaticPos = transform.position;
        m_CanDrag = true;

    }
    public void OnMouseRayHit() {
        //Debug.Log("Cast！");
        if(!m_CanDrag) return;
        if(m_IsDraging) return;
        //if (Input.GetMouseButton(0)) {
            DragStart();
        //}
    }
    void Update() {
        //Debug.Log("MousePos In Screen" + Input.mousePosition);
        //Debug.Log("000 In sreen "+Camera.main.WorldToScreenPoint(new Vector3(0,0,0)));
        //Debug.Log("0010 In sreen "+Camera.main.WorldToScreenPoint(new Vector3(0,0,10)));
        //Debug.Log("00-10 In sreen "+Camera.main.WorldToScreenPoint(new Vector3(0,0,-10)));
        //Debug.Log("Offset" + m_MouseOffsetInWorld);

        if(!m_CanDrag) return;
        if(m_IsDraging) {
            m_CurrMousePosInScreen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, m_CardAxisZInScreen);
            transform.position = Camera.main.ScreenToWorldPoint(m_CurrMousePosInScreen) + m_MouseOffsetInWorld;
            //if(Input.GetMouseButtonUp(0)) {
            //    DragEnd();
            //}
        }
    }
    public void DragStart() {
            //MouseRayCaster.Instance.IsRayCastOpen = false;
            //MouseRayCaster.Instance.SetHolding(CurrCardID);
            m_StaticPos = transform.position;
            m_CardAxisZInScreen = Camera.main.WorldToScreenPoint(transform.position).z;
            m_CurrMousePosInScreen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, m_CardAxisZInScreen);
            m_MouseOffsetInWorld = transform.position - Camera.main.ScreenToWorldPoint(m_CurrMousePosInScreen);
            //Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset;
            m_IsDraging = true;
            //m_OriginSortingOrder = transform.position.z;//GetComponent<SpriteRenderer>().sortingOrder;
            //GetComponent<SpriteRenderer>().sortingOrder = 1000;
            transform.position = new Vector3(transform.position.x, transform.position.y, -1f);
            GetComponent<Collider>().enabled = false;
    }
    public void DragEnd() {
            m_IsDraging = false;
            transform.position = m_StaticPos;
            //GetComponent<SpriteRenderer>().sortingOrder = m_OriginSortingOrder;
            //MouseRayCaster.Instance.IsRayCastOpen = true;
            //.Instance.ResetHolding();
            GetComponent<Collider>().enabled = true;
            GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
    }
    
    public void Hide() {
        //Debug.Log("Hide");
        if(m_AlphaControl == null) {
            m_IsAlphaDescending = true;
            m_AlphaControl = StartCoroutine(AlphaCoroutine());
        } else {
            m_IsAlphaDescending = true;
        }
    }
    public void ReShow() {
        //Debug.Log("Reshow");
        if(m_AlphaControl == null) {
            m_IsAlphaDescending = false;
            m_AlphaControl = StartCoroutine(AlphaCoroutine());
        } else {
            m_IsAlphaDescending = false;
        }
    }
    IEnumerator AlphaCoroutine() {
        float time = 0.2f;
        float percent = 0f;
        float desAlpha = m_IsAlphaDescending ? 0f : 1f;
        while(GetComponent<SpriteRenderer>().color.a != (desAlpha = m_IsAlphaDescending ? 0f : 1f)) {
            float deltaSign = m_IsAlphaDescending ? -1f : 1f;
            GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, Mathf.Clamp(GetComponent<SpriteRenderer>().color.a + deltaSign * Time.deltaTime / time,0f,1f));
            //desAlpha = m_IsAlphaDescending ? 0f : 1f;
            yield return null;
            //Debug.Log("Running" + GetComponent<SpriteRenderer>().color.a);
        }
        m_AlphaControl = null;
    }
}
